﻿/*
创建人：zhangrj
日期：2022-11-21
描述：槽液化验设备检测项维护页面js
*/
var page = {
    url: getRootPath() + "/Ashx/BathResMonitorMaintHandler.ashx", //后台处理url,
    //分页参数
    pageTotal: 0, //分页总数
    pageSize: 10, //每页条数
    pageNumber: 1, //页码
    btnOverStatus: "", //到期按钮状态(0：超期，1：1日到期，3:3日到期)
    init: function () {
        page.bind();
    },
    //绑定事件
    bind: function () {
        customizedEasyuiPagination('pp'); //定制化分页
        //主列表加载
        $("#gdBathResMonitorInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pageTotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'RESWORKCENTERCODE', title: '工作中心', width: 120 },
                 { field: 'RESSOURCEDESC', title: '设备名称', width: 150 },
                 { field: 'EXPIREDATE', title: '到期日期', width: 90,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             var expire = dayjs(value).format('YYYY-MM-DD');
                             if (row.EXPIRETIP <= "0") {
                                 expire = "<span style='color:#802A2A'>" + expire + "</span>";
                             }
                             else if (row.EXPIRETIP > "0" && row.EXPIRETIP <= "1") {
                                 expire = "<span style='color:#ED9121'>" + expire + "</span>";
                             }
                             else if (row.EXPIRETIP > "1" && row.EXPIRETIP <= "3") {
                                 expire = "<span style='color:#32CD32'>" + expire + "</span>";
                             }
                             return expire;
                         }
                     }
                 },
                 { field: 'BATHRESMONITORCHECKNO', title: '送检单号', width: 130 },
                 { field: 'SENDCHECKDATE', title: '送检日期', width: 90,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'STATUSNAME', title: '状态', width: 80 },
                 { field: 'CREATEEMPNAME', title: '创建人', width: 60 },
                 { field: 'CREATEDATE', title: '创建日期', width: 90,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'LASTCHANGEEMPNAME', title: '修改人', width: 60 },
                 { field: 'LASTCHANGEDATE', title: '修改日期', width: 90,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'CHECKDOCPATH', title: '送检报告', width: 150,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return "<a href='" + row.CHECKDOCPATH + "' target='_blank' download='" + row.CHECKDOCFILENAME + "'>" + row.CHECKDOCFILENAME + "</a>";
                         }
                     }
                 },
                 { field: 'CHECKDOCPATH1', title: '上传', width: 120,
                     formatter: function (value, row, index) {
                         return "<button class='easyui-linkbutton' onclick='page.uploadFile(\"" + row.BATHRESMONITORCHECKID + "\")' style='width: 100px; height: 26px'>上传</button>";
                     }
                 },
                 { field: 'BATHRESMONITORCHECKID', hidden: true }
                ]]
        });

        //主列表分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.clearPage(false, true, true);
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        $(document).ready(function () {
            //绑定分页go按钮
            $('#pageGo').on("click", function () {
                page.clearPage(false, true, true);
                var pageNumber = $(this).parents('tr').find('.pagination-num').val();
                page.pageNumber = pageNumber;
                page.search();
            });
        });
        //查询信息
        $("#btnSearch").on("click", function () {
            page.btnOverStatus = "";
            page.clearPage(false, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //重置查询
        $("#btnReset").on("click", function () {
            page.btnOverStatus = "";
            page.clearPage(true, true, true);
        });
        //生成化验单
        $("#btnCreate").on("click", function () {
            page.btnOverStatus = "";
            page.clearPage(false, false, true);
            page.createCheckBill();
        });
        //删除化验单
        $("#btnDele").on("click", function () {
            if (confirm("是否删除")) {
                page.clearPage(false, false, true);
                page.updateCheckBill(1);
            }
        });
        //化验单检验完成
        $("#btnFinish").on("click", function () {
            page.clearPage(false, false, true);
            page.updateCheckBill(2);
        });
        //超期
        $("#btnOverdue").on("click", function () {
            page.btnOverStatus = "0";
            page.clearPage(false, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //1日到期
        $("#btnOneDayOverdue").on("click", function () {
            page.btnOverStatus = "1";
            page.clearPage(false, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //3日到期
        $("#btnThreeDaysOverdue").on("click", function () {
            page.btnOverStatus = "3";
            page.clearPage(false, true, true);
            page.pageNumber = 1;
            page.search();
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetBathResMonitorCheckList",
            pageSize: page.pageSize,
            pageIndex: page.pageNumber,
            workcentercode: $("#txtWorkCenterCode").textbox("getValue"),
            resource: $("#txtResource").textbox("getValue"),
            status: $("#selStatus").combobox("getValue"),
            sendCheckDate: $("#txtSendCheckDate").datebox("getValue"),
            changeDate: $("#txtChangeDate").datebox("getValue"),
            overduestatus: page.btnOverStatus //到期按钮状态(0：超期，1：1日到期，3:3日到期)
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pageTotal = res.RowCount;
            $("#gdBathResMonitorInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //生成检验单
    createCheckBill: function () {
        var selected = $("#gdBathResMonitorInfo").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("请选择数据", false);
            return false;
        }
        var datas = {
            action: "CreateCheckBill",
            paramsDt: JSON.stringify(selected)
        }
        page.post(page.url, datas, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //更改检验单状态
    updateCheckBill: function (type) {
        var selected = $("#gdBathResMonitorInfo").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("请选择数据", false);
            return false;
        }
        var isValid = true;
        var isDoc = true;
        if (type === 2) {
            for (var i = 0; i < selected.length; i++) {
                if (isNullOrWhiteSpaceStr(selected[i].CHECKDOCPATH)) {
                    isDoc = false;
                }
                if (selected[i].STATUS === "5") {
                    isValid = false;
                }
            }
        }
        if (!isValid) {
            displayMessage("存在已送检完成的检验单", false);
            return false;
        }
        if (!isDoc && !confirm("存在没有上传检验报告的数据，是否继续完成")) {
            return;
        }
        var datas = {
            action: "UpdateBathResMonitorCheckStatus",
            paramsDt: JSON.stringify(selected),
            operaType: type
        }
        page.post(page.url, datas, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //上传
    uploadFile: function (rowID) {
        //判断是否ie浏览器
        if (window.ActiveXObject || "ActiveXObject" in window) {
            displayMessage("请用谷歌浏览器进行上传操作", false);
            return;
        }
        $("#picker").click();
        //文件选择
        $("#picker").on("change", function () {
            var files = $('#picker')[0].files[0];
            if (isNullOrWhiteSpaceStr(files)) {
                return;
            }
            var formdata = new FormData();
            formdata.append('file', files);
            formdata.append('action', "UploadCheckFile");
            formdata.append('bathResmonitorCheckID', rowID);
            $.ajax({
                type: "Post",
                url: page.url,
                data: formdata,
                contentType: false,
                processData: false,
                cashe: false,
                async: true,
                dataType: "json",
                beforeSend: function () {
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (res) {
                    $.messager.progress('close');
                    if (res.Result) {
                        $("#btnSearch").click();
                    }
                    $("#picker").val("");
                    displayMessage(res.Message, res.Result);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.messager.progress('close');
                    displayMessage(XMLHttpRequest.responseText, false);
                }
            });
        });
    },
    //清除页面（查询区域，列表区域，是否清空消息）
    clearPage: function (query, list, msg) {
        if (query) {
            $("#txtWorkCenterCode").textbox("setValue", ""); //工作中心
            $("#txtResource").textbox("setValue", ""); //设备
            $("#selStatus").combobox("setValue", ""); //状态
            $("#txtSendCheckDate").datebox("setValue","");
            $("#txtChangeDate").datebox("setValue","");
        }
        if (list) {
            clearEasyuiDataGrid("gdBathResMonitorInfo"); //清空列表
        }
        if (msg) {
            clearMessage();
        }
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
}
$(document).ready(function () {
    page.init();
});